/**
 * Created by Mars on 17/4/12.
 */
function Dragger() {
    this.Init();
};

Dragger.prototype.Init = function () {
    this.Event();
};

Dragger.prototype.Event = function () {
    var _this = this;

    var target = $('.dragger');

    //拖拽经过区域
    target.on('dragover', function (e) {
        e.preventDefault();
        // console.log('over');
    });
    //拖拽松开
    target.on('drop', function(e) {
        // console.log('drop');
        e.preventDefault();
        //获取拖拽信息
        var id = e.originalEvent.dataTransfer.getData('id');
        // console.log(id);

        //获取拖拽对象
        var obj = $('#material_'+id);
        //获取拖拽对象的类型
        var type = obj.data('type');

        // 判断类型
        switch (type){
            case 'video':
                _this.AddToVideo(this,id);
                break;
            case 'music':

                break;
            case 'img':

                break;
        }

    });
    
    
    target.on('mousedown',function (e) {
        // console.log('click');
        var x = e.clientX + target.scrollLeft() + document.body.scrollLeft;
        $('.videotimescale-stdruler').css({left:x});
    });

    $('.videotimescale').on('click',function (e) {
        // console.log('click2');
        var x = e.clientX + target.scrollLeft() + document.body.scrollLeft;
        $('.videotimescale-stdruler').css({left:x});
        // return true
    });

    $('.videotimescale-stdruler').on('mousedown', function(e) {
        var x = e.clientX + target.scrollLeft() + document.body.scrollLeft;
        $('.videotimescale-stdruler').css({left:x});
    });


    // target.on('mousemove',function (e) {
    //     $('.videotimescale-stdruler-black').show();
    //     var x = e.clientX + target.scrollLeft() + document.body.scrollLeft;
    //     $('.videotimescale-stdruler-black').css({left:x});
    // });
    // target.on('mouseleave',function (e) {
    //     $('.videotimescale-stdruler-black').hide();
    // });

    // $('.videotimescale').on('mouseover',function (e) {
    //     var x = e.clientX + target.scrollLeft() + document.body.scrollLeft;
    //     $('.videotimescale-stdruler').css({left:x});
    // });
};

Dragger.prototype.AddToVideo = function (target,id) {
    var _this = this;
    var data = findDataByid(id);
    var html = _this.TemplateVideo(data);
    $('.dragger-videos').append(html);
}

Dragger.prototype.TemplateVideo = function (data) {
    var img = data.head_img;
    var id = data.material_id;
    var width = getVideoWidth(data.info.seconds);

    var arr = [];

    //判断视频轨道是否已经有视频,有的话添加间隔图片
    if($('.dragger-videos').children().length>0){
        arr.push(' <div class="dragger-videos-division"></div>');
    }else {
        player.src(data.src);
    }

    //加入到播放列表中
    videoList.push(data);

    arr.push('<div draggable="true" id="material_'+id+'" data-type="video" class="dragger-videos-video" style="width:'+ width +'px;background-image: url('+img+')">');
    arr.push('</div>');
    return arr.join('');
}